/*******************************************************************
 * author   :  Charlies-ls
 * Date     :  2021-09-26
 * Copyright (c) 2021. artestplay@outlook.com. All Rights Reserved.
 *******************************************************************/
#include "ax_printf.h"
#define AX_PRINTF_MAX_BUF 1024
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
void ax_printf(const char* color,const char* fmt,...)
{
   char buf[AX_PRINTF_MAX_BUF];
   va_list args;
   int ret;
   ret=snprintf(buf,AX_PRINTF_MAX_BUF,"%s%s%s",color,fmt,AX_COLOR_RESET);
   if(ret>AX_PRINTF_MAX_BUF)
   {
   	printf("%s printf buf is full!%s\n",AX_LIGHT_RED,AX_COLOR_RESET);
   }
   va_start(args, buf);
   vprintf(buf, args);
   va_end(args);
}

void ax_error_exit(const char* fmt,...)
{
   char buf[AX_PRINTF_MAX_BUF];
   va_list args;
   int ret;
   ret=snprintf(buf,AX_PRINTF_MAX_BUF,"%s%s%s",AX_PERROR,fmt,AX_COLOR_RESET);
   if(ret>AX_PRINTF_MAX_BUF)
   {
      printf("%s printf buf is full!%s\n",AX_LIGHT_RED,AX_COLOR_RESET);
   }
   va_start(args, buf);
   vprintf(buf, args);
   va_end(args); 
   exit(0);
}
void ax_info(const char*fmt,...)
{
  char buf[AX_PRINTF_MAX_BUF];
   va_list args;
   int ret;
   ret=snprintf(buf,AX_PRINTF_MAX_BUF,"%s%s%s",AX_LIGHT_YELLOW,fmt,AX_COLOR_RESET);
   if(ret>AX_PRINTF_MAX_BUF)
   {
      printf("%s printf buf is full!%s\n",AX_LIGHT_RED,AX_COLOR_RESET);
   }
   va_start(args, buf);
   vprintf(buf, args);
   va_end(args); 
}
void ax_debug(const char*fmt,...)
{
   char buf[AX_PRINTF_MAX_BUF];
   va_list args;
   int ret;
   ret=snprintf(buf,AX_PRINTF_MAX_BUF,"%s%s%s",AX_LIGHT_GREEN,fmt,AX_COLOR_RESET);
   if(ret>AX_PRINTF_MAX_BUF)
   {
      printf("%s printf buf is full!%s\n",AX_LIGHT_RED,AX_COLOR_RESET);
   }
   va_start(args, buf);
   vprintf(buf, args);
   va_end(args);
}
void ax_error(const char*fmt,...)
{
   char buf[AX_PRINTF_MAX_BUF];
   va_list args;
   int ret;
   ret=snprintf(buf,AX_PRINTF_MAX_BUF,"%s%s%s",AX_LIGHT_RED,fmt,AX_COLOR_RESET);
   if(ret>AX_PRINTF_MAX_BUF)
   {
      fprintf(stderr,"%s printf buf is full!%s\n",AX_LIGHT_RED,AX_COLOR_RESET);
   }
   va_start(args, buf);
   vprintf(buf, args);
   va_end(args);
}

void ax_printf_hex(const uint8_t* hex,const int len,const char* tips)
{
   ax_printf(AX_LIGHT_YELLOW,"len: %d %s:\n",len,tips);
   for(int i=0;i<len;i++)
   {
      printf("%02X ",hex[i]);
      if((i+1%16)==0)
         printf("\n");
   }
   printf("\n");
}

void ax_printbytes(const unsigned char*bytes,const int len,const char* tips)
{
   ax_printf(AX_LIGHT_YELLOW,"len: %d %s:\n",len,tips);
   for(int i=0;i<len;i++)
   {
      printf("%d ",bytes[i]);
      if((i+1%32)==0)
         printf("\n");
   }
   printf("\n");

}

void ax_printints(const int *datas,const int len,const char* tips)
{
    ax_printf(AX_LIGHT_YELLOW,"len: %d %s:\n",len,tips);
    for(int i=0;i<len;i++)
    {
       printf("%d ",datas[i]);
       if((i+1%32)==0)
          printf("\n");
    }
    printf("\n");
}
void ax_print_floats(const float* datas,const int len,const char* tips)
{
    ax_printf(AX_LIGHT_YELLOW,"len: %d %s:\n",len,tips);
    for(int i=0;i<len;i++)
    {
       printf("%f ",datas[i]);
    }
    printf("\n");
}
void ax_print_doubles(const double* datas,const int len,const char* tips)
{
    ax_printf(AX_LIGHT_YELLOW,"len: %d %s:\n",len,tips);
    for(int i=0;i<len;i++)
    {
       printf("%f ",datas[i]);
    }
    printf("\n");
}
/**
 * @brief ax_print_binarybyte
 * @param byte
 * @param tips
 */
void ax_print_binarybyte(const uint8_t byte,const char* tips)
{
    ax_info("%s\n",tips);
    uint8_t data = byte;
    for(int i=0;i<8;i++)
    {
        uint8_t v = (data & 0x80) >> 7;
        data = data << 1;
        ax_info("%d",v);
    }
    ax_info("%s","\n");
}
void ax_print_binaryuint16(const uint16_t word,const char* tips)
{
    ax_info("%s\n",tips);
    uint16_t data = word;
    for(int i=0;i<16;i++)
    {
        uint16_t v = (data & 0x8000) >> 15;
        data = data << 1;
        ax_info("%d",v);
        if( (i+1) % 8==0)
            ax_info("%s"," ");
    }
    ax_info("%s","\n");

}
void ax_print_binary_dword(const uint32_t dword,const char* tips)
{
    ax_info("%s\n",tips);
    uint32_t data = dword;
    for(int i=0;i<32;i++)
    {
        uint32_t v = (data & 0x80000000) >> 31;
        data = data << 1;
        ax_info("%d",v);
        if( (i+1)%8==0)
            ax_info("%s"," ");
    }
    ax_info("%s","\n");
}
void ax_print_binary_dwords(const uint32_t *dword, const int len,const char* tips)
{
    ax_info("%s\n",tips);

    for(int i=0;i<len;i++)
    {
        uint32_t data = dword[i];
        for(int i=0;i<32;i++)
        {
            uint32_t v = (data & 0x80000000) >> 31;
            data = data << 1;
            ax_info("%d",v);
            if( (i+1)%8==0)
                ax_info("%s"," ");
        }
    }

    printf("\n");
}

void ax_print_binarybytes(const char* bytes,const int len,const char* tips)
{
    ax_info("bytes len:%d ",len);

    ax_info("%s\n", tips);

    for(int index = 0;index<len;index++)
    {
        uint8_t data = bytes[index];
        for(int i=0;i<8;i++)
        {
            uint8_t v = (data & 0x80) >> 7;
            data = data << 1;
            ax_info("%d",v);
        }

        ax_info(" ");
        if((index+1)%16==0)
        {
            ax_info("\n");
        }

    }
    printf("\n");
}
